package org.hismig.view;

import java.awt.Component;
import java.awt.Toolkit;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.hismig.migration.result.DBResultInfo;
import org.hismig.migration.result.JobResultInfo;
import org.hismig.migration.result.ResultInfo;
import org.hismig.migration.result.RunResultInfo;
import org.hismig.migration.result.TableResultInfo;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-8-17 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class ResultTreeCellRenderer extends DefaultTreeCellRenderer {

	private static final long serialVersionUID = 2864530655482145863L;

	private Icon jobIcon;
	private Icon jobErrorIcon;

	private Icon errorIcon;
	private Icon successIcon;

	private Icon dbSuccessIcon;
	private Icon dbErrorIcon;

	private Icon tableSuccessIcon;
	private Icon tableErrorIcon;

	private Icon computerIcon;

	public ResultTreeCellRenderer() {
		initIcons();
	}

	private void initIcons() {
		jobErrorIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("folderError.png")));
		jobIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("folder.png")));
		successIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("success.png")));
		errorIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("error.png")));

		dbSuccessIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("db_success.png")));

		dbErrorIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("db_error.png")));

		tableSuccessIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("table_success.png")));

		tableErrorIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("table_error.png")));

		computerIcon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("computer.png")));

	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
				row, hasFocus);

		Object userObj = null;

		if (value instanceof DefaultMutableTreeNode) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

			userObj = node.getUserObject();
		}

		if (null == userObj) {
			setToolTipText(null);
		} else if (userObj instanceof JobResultInfo) {
			JobResultInfo info = (JobResultInfo) userObj;

			if (info.getErrorCount() > 0) {
				setIcon(jobErrorIcon);
			} else {
				setIcon(jobIcon);
			}

			setToolTipText(info.formate());
		} else if (userObj instanceof DBResultInfo) {
			DBResultInfo info = (DBResultInfo) userObj;

			if (info.getErrorCount() > 0) {
				setIcon(dbErrorIcon);
			} else {
				setIcon(dbSuccessIcon);
			}

			setToolTipText(info.formate());
		} else if (userObj instanceof TableResultInfo) {
			TableResultInfo info = (TableResultInfo) userObj;

			if (info.getErrorCount() > 0) {
				setIcon(tableErrorIcon);
			} else {
				setIcon(tableSuccessIcon);
			}
			setToolTipText(info.formate());
		} else if (userObj instanceof ResultInfo) {
			ResultInfo info = (ResultInfo) userObj;

			if (info.getErrorCount() > 0) {
				setIcon(errorIcon);
			} else {
				setIcon(successIcon);
			}
			setToolTipText(info.formate());
		} else if (userObj instanceof RunResultInfo) {
			setIcon(computerIcon);
			setToolTipText(((RunResultInfo) userObj).formate());
		}

		return this;
	}

}
